package com.sharewithus.jms.util;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.apache.log4j.Logger;

import sharewithus.business.dto.RelationShipMsg;

import com.sharewithus.dao.IRelationShipDBDAO;

/** 
 * @ClassName: RelationQueueListener 
 * @Description: 
 * @author chenyun
 * @date 2012-5-17 上午11:37:12 
 */
public class RelationQueueListener implements MessageListener {
	
	static Logger log = Logger.getLogger(RelationQueueListener.class);
	
	private IRelationShipDBDAO dbDao;
	
	public void onMessage(Message message) {
		if(message instanceof ObjectMessage) {
			
			ObjectMessage objMsg = (ObjectMessage) message;
			try {
				RelationShipMsg rs = (RelationShipMsg) objMsg.getObject();
				
				if("ADD".equals(rs.getType())) {
					dbDao.addRelationShip(rs.getUserId(), rs.getFollower());
				} else if("CANCEL".equals(rs.getType())) {
					dbDao.removeRelationShip(rs.getUserId(), rs.getFollower());
				}
				
			} catch (JMSException e) {
				e.printStackTrace();
				log.error("do jmsException", e);
			} catch (Exception e) {
				e.printStackTrace();
				log.error("db or other Exception", e);
			}
			
		} else {
			log.warn("the Message is not ObjectMessage");
		}
	}

	public IRelationShipDBDAO getDbDao() {
		return dbDao;
	}

	public void setDbDao(IRelationShipDBDAO dbDao) {
		this.dbDao = dbDao;
	}
	
}

